﻿// JScript 文件

//--------------------附件上传3--Start------------------   

function Attach(dispTextContainer, tag)
{
    var fileIndex = 1;
    if (tag == null || tag == "")
    {
        tag = "uploadImg";
    } 

    //初始化第一个
    document.write('<a id="MyFile" class="addfile" hidefocus="hidefocus" href="javascript:;"><input class="addfile" hidefocus="hidefocus" id="' + tag + '0" name="' + tag + '0" type="file" /></a>');
    var currentFile = document.getElementById(tag + "0"); //当前显示的文本域
    currentFile.onchange = AddFile;
    function AddFile()
    {
        
        var container = document.getElementById(dispTextContainer); //用于显示附件名称的容器
        var newFile = document.createElement("input");
        newFile.type = "file";
        newFile.className = "addfile";
        newFile.hidefocus = "hidefocus";
        newFile.id = newFile.name = tag + fileIndex;       
        newFile.onchange = AddFile;

        var fileName = currentFile.value.split("\\").pop();
        currentFile.parentNode.appendChild(newFile);
        currentFile.style.display = "none"; //上一个设为不可见

        //把文件名显示出来
        var strAppPath = getAppPath();
        var preFileIndex = fileIndex - 1; //上一次文件域索引
        var spanText = document.createElement("span");
        spanText.id = "disp" + tag + preFileIndex;
        spanText.innerHTML = "<img src='" + strAppPath + "common/images/common/attach.gif' alt=''/>&nbsp;" + fileName +
                     "&nbsp;<img id='img" + tag + preFileIndex + "' src='" + strAppPath + "common/Images/common/delAttach.GIF'  align='absmiddle' style='cursor:pointer' border='0'>;&nbsp;";
                     
        container.appendChild(spanText);

        document.getElementById("img" + tag + preFileIndex).onclick = function()
        {
            DeleteFile(preFileIndex);
        };


        fileIndex++;
        currentFile = newFile;
    }

    function DeleteFile(index)
    {       
        var spText = document.getElementById("disp" + tag + index); //显示的文本
        spText.parentNode.removeChild(spText);
        document.getElementById("MyFile").removeChild(document.forms[0][tag + index]); //从元素P上删除子结点 a 。（跟删除表格行同步）

    }
}

//container：容器 tag：标识，用于多组图片上传
function ImageUpload(container, tag)
{
    var imgIndex = 1;
    if (tag == null || tag == "")
    {
        tag = "uploadImg";
    }
    document.write('<a id="' + tag + '" class="addfile addimg" hidefocus="hidefocus" href="javascript:;"><input class="addfile" hidefocus="hidefocus" id="' + tag + '0" name="' + tag + '0" type="file" /></a>');
    var currentFile = document.getElementById(tag + "0"); //当前显示的文本域
    currentFile.onchange = AddImg;
    function AddImg()
    {
        if (typeof (container) == "string")
        {
            container = document.getElementById(container); //用于显示附件名称的容器
        }
        var newFile = document.createElement("input");
        newFile.type = "file";
        newFile.className = "addfile";
        newFile.hidefocus = "hidefocus";
        newFile.id = newFile.name = tag + imgIndex;
        newFile.onchange = AddImg;

        var fileName = currentFile.value.split("\\").pop();
        currentFile.parentNode.appendChild(newFile);
        currentFile.style.display = "none"; //上一个设为不可见

        //把文件名显示出来
        var strAppPath = getAppPath();
        var preFileIndex = imgIndex - 1; //上一次文件域索引
        var spanText = document.createElement("span");
        spanText.id = "disp" + tag + preFileIndex;
        spanText.innerHTML = fileName + "&nbsp;<img id='img" + preFileIndex + "' src='" + strAppPath + "common/Images/common/delAttach.GIF'  align='absmiddle' style='cursor:pointer' border='0'>;&nbsp;";

        container.appendChild(spanText);

        document.getElementById("img" + preFileIndex).onclick = function()
        {
            DeleteFile(preFileIndex);
        };


        imgIndex++;
        currentFile = newFile;

        if (ImageUpload.ImageAdded != null)
        {
            ImageUpload.ImageAdded(tag + (imgIndex-2));
        }
        
    };

    function DeleteFile(index)
    {
        var spText = document.getElementById("disp" + tag + index); //显示的文本
        spText.parentNode.removeChild(spText);
        document.getElementById(tag).removeChild(document.forms[0][tag + index]); //从元素P上删除子结点 a 。（跟删除表格行同步）

    }
}
